#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <qqthostaddressmanager.h>

MainWindow::MainWindow ( QWidget* parent ) :
    QMainWindow ( parent ),
    ui ( new Ui::MainWindow )
{
    ui->setupUi ( this );

    b0 = new QQtBeServerManager ( this );
    b0->setListeningPort ( 6500 );

    QQtHostAddressManager m0;
    QString ip = m0.getHostAddress();
    b0->setTellClient ( ip, 6510 );

    dc0 = new QQtDataCache ( this );
    dc0->setListeningPort ( 6510 );
    mDict["Value"] = 0;
    connect ( dc0, SIGNAL ( signalRecvValue ( int ) ),
              this, SLOT ( slotRecvValue ( int ) ) );

    plot1 = ui->widget;
    plot1->xAxis->setTickLabelType ( QCPAxis::ltNumber );
    plot1->xAxis->setAutoTickStep ( true );

    g1 = plot1->addGraph();
    g1->setPen ( QPen ( Qt::blue ) );
    g1->setName ( "Value" );
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::slotRecvValue ( int value )
{
    int vvvv = mDict["Value"].getValue().toInt();
    vvvv = vvvv + value;
    p3line() << vvvv;
    mDict["Value"] = vvvv;
    ui->textBrowser->clear();
    ui->textBrowser->append ( QString::number ( vvvv ) );

    static int i = 0;
    g1->addData ( i++, vvvv );
    g1->rescaleAxes ( true );
    plot1->xAxis->setRange ( 0, i,  Qt::AlignLeft );
    plot1->replot();
}
